/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.shef.speech_r25;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author samf
 */
public class Worksheet {

    WorkImpl workImpl;
    PrintStream out;
    public Worksheet() {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd-HH:mm:ss");
            Date date = new Date();
            out = new PrintStream("out"+dateFormat.format(date)+".txt");
            workImpl = new WorkImpl("resources/corpus.txt",
                "resources/answers.txt", out);

        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    void simulate(String asrFile) {
        try {
            BufferedReader r = new BufferedReader(new FileReader(asrFile));
            String line;
            while ((line = r.readLine()) != null) {
                out.println("input from ASR: '"+line+"'");
                System.out.println("input from ASR: '"+line+"'");
                
                Segment segment = workImpl.findBest(line);
                out.println("best segment: '"+segment+"'");
                //out.println("input from ASR")
                if (segment.end < workImpl.prevPos) {
                    //System.out.println("prob "+segment.toString(worksheet.wordSeq)+" "+segment.end+" "+worksheet.prevPos);
                    out.println("bad segment (gone back?): '"+segment+"'");
              
                } else {
                    //System.out.println(workImpl.currentPos + " " + segment.toString(workImpl.wordSeq));
                    String response = workImpl.respond(segment);
                    out.println("Response: '"+ response +"'");
              //      out.println(line);
                    System.out.println("Response: '"+ response +"'");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Worksheet w = new Worksheet();
        //w.simulate("resources/asr-wsj.txt");
        w.simulate("resources/sil_asr.txt");

    }
}
